package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.OrderHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.utils.FallbackMethod;
import javafx.beans.DefaultProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @USER: dayu Administrator
 * @DATE: 2020/3/27
 * @DESCRIPTION:
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "paymentGlobalFallback")
public class HystrixOrderController {

    @Resource
    OrderHystrixService orderHystrixService;

    @GetMapping("/consumer/payment/get/{id}")
    public String getOk(@PathVariable("id") Long id) {
        String result = orderHystrixService.getOk(id);
        log.info("********"+result+"**********");
        return result;
    }

    @GetMapping("/consumer/payment/timeout/{id}")
    public String getTimeout(@PathVariable("id") Long id) {
        String result = orderHystrixService.getTimeout(id);
        log.info("***timeout********"+result+"**********");
        return result;
    }


    @GetMapping("/consumer2/payment/timeout/{id}")
//    @HystrixCommand(fallbackMethod = "getTimeoutWithHystrixCommandHandler",commandProperties = {
//            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "1500")
//    })
    @HystrixCommand
    public String getTimeoutWithHystrixCommand(@PathVariable("id") Long id) {
        String result = orderHystrixService.getTimeout(id);
        log.info("***timeout********"+result+"**********");
        return result;
    }

    public String getTimeoutWithHystrixCommandHandler(@PathVariable("id") Long id) {
        return "我是消费者80，方法已经延时或出现异常，请过一段时间再刷新尝试一下！！！！！";
    }


    public String paymentGlobalFallback(){
        return "消费者客户端的默认处理方法,你调用的方法出现了故障或者超时了，等会再试试吧";
    }

}
